package com.maxuxu.scihome.controller.notice;

import com.maxuxu.scihome.mapper.NoticeMapper;
import com.maxuxu.scihome.pojo.Message;
import com.maxuxu.scihome.pojo.Notices;
import com.maxuxu.scihome.pojo.Pages;
import com.maxuxu.scihome.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: NoticeController
 * @author: Maxuxu
 * @date: 2022/7/5 10:25
 * @description: 公告的控制类
 */
@RestController
@Slf4j
public class NoticeController {
    @Autowired
    Notices notices;
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    NoticeService noticeService;
    @Autowired
    Message message;


    @PostMapping("/notice/insert")
    public Message insert(HttpServletRequest request){
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        notices.setTitle(title);
        notices.setContent(content);
        noticeService.insertNotices(notices);
        message.setJson("插入成功");
        return message;
    }

    @PostMapping("/notice/select")
    public Pages findAll(long current, long size) {
        return noticeService.selectPage(current, size);
    }


    @GetMapping("/notice/removeNotice")
    public Message removenotice(HttpServletRequest request) {
        String ids = request.getParameter("ids").trim();
        String[] split = ids.split(",");
        ArrayList<Integer> list = new ArrayList<>();
        for (String s:
                split) {
            Integer target = Integer.valueOf(s);
            list.add(target);
        }
        noticeService.deletnotices(list);
        message.setJson("删除成功"+ids);
        return message;
    }

    @GetMapping("/notice/selectone")
    public Notices findone(HttpServletRequest request){
        String name = request.getParameter("title");
        log.error(name);
        Notices Notice1 = new Notices();
        Notice1.setTitle(name);
        return noticeService.selectByAny(Notice1);
    }

    @GetMapping("/notice/selectone2")
    public List<Notices> findone2(HttpServletRequest request){
        String content = request.getParameter("content");
        return noticeService.selectByLike(content);
    }

    @PostMapping("/notice/update")
    public Notices update(String old,String title, String content) {
        log.error("文档的旧标题是：{}，名字是:{},评论是：{}", old, title, content);
        notices.setTitle(title);
        notices.setContent(content);
        noticeService.updateNotice(old, notices);
        return notices;
    }
}
